package com.thkj.util;

import java.awt.Color;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;

public class Util {

	public static Logger logger = Logger.getLogger(Util.class);

	/**
	 * 获取当前timestamp
	 * 
	 * @return
	 */
	public static Timestamp getCurrTimestamp() {
		return new Timestamp(System.currentTimeMillis());
	}
	
	public static Long getTime() {
		Date date = new Date();
		String dateStr = new SimpleDateFormat("yyyyMMddHHmmssSSS").format(date);
		return Long.parseLong(dateStr);
	}

	public static Color toColorFromString(String colorStr) {
		if (!(colorStr.length() == 7)) {
			return null;
		} else {
			String str1 = colorStr.substring(1, 3);
			String str2 = colorStr.substring(3, 5);
			String str3 = colorStr.substring(5, 7);
			int red = Integer.parseInt(str1, 16);
			int green = Integer.parseInt(str2, 16);
			int blue = Integer.parseInt(str3, 16);
			Color color = new Color(red, green, blue);
			return color;
		}
	}

	/**
	 * 生成4位随机字母和数字组合
	 */
	public static String generateWord() {
		String[] beforeShuffle = new String[] { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d",
				"e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y",
				"z", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T",
				"U", "V", "W", "X", "Y", "Z" };
		List list = Arrays.asList(beforeShuffle);
		Collections.shuffle(list);
		StringBuilder sb = new StringBuilder();
		for (int i = 0; i < list.size(); i++) {
			sb.append(list.get(i));
		}
		String afterShuffle = sb.toString();
		String result = afterShuffle.substring(5, 9);
		return result;
	}

	public static String generateShortUUID() {
		String[] beforeShuffle = new String[] { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T",
				"U", "V", "W", "X", "Y", "Z" };
		List list = Arrays.asList(beforeShuffle);
		Collections.shuffle(list);
		StringBuilder sb = new StringBuilder();
		for (int i = 0; i < list.size(); i++) {
			sb.append(list.get(i));
		}
		String afterShuffle = sb.toString();
		String result = afterShuffle.substring(0,8);
		return result;
	}

	/**
	 * 验证手机规则
	 * 
	 * @param phone
	 * @return
	 */
	public static boolean checkPhone(String phone) {
		String value = phone;
		String regExp = "^[1][0-9]{10}$";
		Pattern p = Pattern.compile(regExp);
		Matcher m = p.matcher(value);
		return m.find();
	}

	public static boolean isMobileDevice(String requestHeader) {
		String[] deviceArray = new String[] { "android", "mac os", "windows phone" };
		if (requestHeader == null)
			return false;
		requestHeader = requestHeader.toLowerCase();
		for (int i = 0; i < deviceArray.length; i++) {
			if (requestHeader.indexOf(deviceArray[i]) > 0) {
				return true;
			}
		}
		return false;
	}

	/**
	 * 获取ip
	 *
	 * @param request
	 * @return
	 */
	public static String getIp(HttpServletRequest request) {
		String ip = request.getHeader("x-forwarded-for");
		request.getHeaderNames();
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("Proxy-Client-IP");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("WL-Proxy-Client-IP");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getRemoteAddr();
		}
		if (!StringUtils.isBlank(ip)) {
			ip = ip.split(",")[0];
		}
		return ip;
	}
	
	public static void main(String[] args) {
		System.out.println(generateShortUUID());
	}
	 public static Map<String,Object> getStringToMap(String str){
	       //根据逗号截取字符串数组
	       String[] str1 = str.split(",");
	       //创建Map对象
	       Map<String,Object> map = new HashMap<>();
	       //循环加入map集合
	       for (int i = 0; i < str1.length; i++) {
	           //根据":"截取字符串数组
	           String[] str2 = str1[i].split(":");
	           map.put(str2[0],str2[1]);
	       }
	       return map;
	 }
	 
		/**
		 * 取文件后缀
		 * 
		 * @param img_path
		 * @return
		 */
		public static String getPicSuffix(String img_path) {
			if (img_path == null || img_path.indexOf(".") == -1) {
				return ""; // 如果图片地址为null或者地址中没有"."就返回""
			}
			return img_path.substring(img_path.lastIndexOf(".") + 1).trim().toLowerCase();
		}
}
